package com.atguigu.lease.web.admin.schedule;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class ScheduledTasks {
    @Autowired
    private LeaseAgreementService leaseAgreementService;

    @Scheduled(cron = "* * 3 * * *")
    public void checkLeaseAgreement() {
        LambdaUpdateWrapper<LeaseAgreement> updateWrapper = new LambdaUpdateWrapper<>();
        //获取系统时间
        Date now = new Date();
        //设置更新条件，租约日期<=当前时间（已经过期的租约）
        updateWrapper.le(LeaseAgreement::getLeaseEndDate,now);
        //设置更新条件，当前装备必须是已签约的租约状态（找不到已过期的租约，防止重复处理）
        updateWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED);
        //设置更新字段，符合条件的租约状态修改为已过期
        updateWrapper.set(LeaseAgreement::getStatus,LeaseStatus.EXPIRED);
        leaseAgreementService.update(updateWrapper);
    }
}
